#!/bin/sh

main()
{
. $DOCUMENT_ROOT/apps/home/header.tpl.sh
header_html
cat <<'EOF'

    <div class="content" id="refresh_short_info">
EOF
cat <<EOF
		<div class="pure-g">
			<div class="pure-u-1-3"></div>
			<div class="pure-u-1-3"><br/><br/><br/><h1>Loading...</h1><br/><br/><br/></div>
			<div class="pure-u-1-3"></div>
		</div>
    </div>
EOF

cat <<'EOF'
<script>
$(function(){
  $('#restart_os').on('click', function(e){
  var url = 'index.cgi?app=home&action=pre_restart_os';
  Ha.common.ajax(url, 'html', '', 'get', '', function(data){
    $('#quick_tool').html(data);
  }, 1);
});
});
$(function(){
  $('#system_log').on('click', function(e){
  var url = 'index.cgi?app=home&action=pre_system_log';
  Ha.common.ajax(url, 'html', '', 'get', '', function(data){
    $('#quick_tool').html(data);
  }, 1);
});
});
$(function(){
  $('#resrart_firewall').on('click', function(e){
  var url = 'index.cgi?app=home&action=pre_resrart_firewall';
  Ha.common.ajax(url, 'html', '', 'get', '', function(data){
    $('#quick_tool').html(data);
  }, 1);
});
});
$(function(){
  $('#restart_network').on('click', function(e){
  var url = 'index.cgi?app=home&action=pre_restart_network';
  Ha.common.ajax(url, 'html', '', 'get', '', function(data){
    $('#quick_tool').html(data);
  }, 1);
});
});
$(function(){
  $('#clean_mem').on('click', function(e){
  var url = 'index.cgi?app=home&action=pre_clean_mem';
  Ha.common.ajax(url, 'html', '', 'get', '', function(data){
    $('#quick_tool').html(data);
  }, 1);
});
});
$(function(){
  $('#netstat').on('click', function(e){
  var url = 'index.cgi?app=home&action=pre_netstat';
  Ha.common.ajax(url, 'html', '', 'get', '', function(data){
    $('#quick_tool').html(data);
  }, 1);
});
});

</script>
EOF

cat <<EOF

    <div class="ribbon l-box-lrg pure-g">
        <div class="l-box-lrg is-center pure-u-1 pure-u-md-1-2 pure-u-lg-3-5">
			  <div class="content" style="background-color: #2F4F4F;text-align:left;font-size:14px;line-height: 90%" id="quick_tool">
				<pre>`pre_clean_mem`</pre>
			  </div>
        </div>
        <div class="pure-u-1 pure-u-md-1-2 pure-u-lg-2-5">

            <h2 class="content-head content-head-ribbon">快捷面板</h2>
            <p>快捷处理事件</p>
			<div class="pure-g">
				<div class="pure-u-1-3">
					<button class="pure-button" id="restart_os">重启系统</button><p>
				</div>
				<div class="pure-u-1-3">
					<button class="pure-button" id="restart_network">重启网络</button><p>
				</div>
				<div class="pure-u-1-3">
					<button class="pure-button" id="resrart_firewall">重启防火墙</button><p>
				</div>
				<div class="pure-u-1-3">
					<button class="pure-button" id="clean_mem">清理内存</button><p>
				</div>
				<div class="pure-u-1-3">
					<button class="pure-button" id="netstat">端口占用</button><p>
				</div>
				<div class="pure-u-1-3">
					<button class="pure-button" id="system_log">系统日志</button><p>
				</div>
			</div>
			
			
			
			
			
			

        </div>
    </div>

    <div class="content">
        <h2 class="content-head is-center">应 用 列 表</h2>
        <div class="pure-g">

<style>	
.heading {
  text-align:left;
  border-bottom:1px solid #d5d5d5;
}
.heading h1 {
  display:inline-block;
  font-weight:100;
  font-size:30px;
  position:relative;
  top:28px;
  background:#fff;
  padding:0 12px;
}
.losticon
{
width:64px;
height:64px;
background-color:#ffffff;
-moz-box-shadow: 10px 10px 5px #888888; /* 老的 Firefox */
box-shadow: 10px 10px 5px #888888;
}
</style>

EOF

home_data=`cat /tmp/home.json`
echo "$home_data" | grep -q '"apps":'
if
[ $? -ne 0 ]
then
main.sbin home_index
home_data=`cat /tmp/home.json`
fi
. /usr/share/libubox/jshn.sh
json_load "$home_data"
json_get_keys apps apps
for type in $apps
do
cat <<EOF
			<div class="pure-u-1">
			<div class="heading">
				<h1>`json_select && json_select i18n && json_select type && json_select ${type} && json_get_var lang "$lang" && echo $lang`</h1>
			</div><br/>
			</div>
EOF
json_select
json_select apps && json_get_keys apps_in_type ${type}
	for app in $apps_in_type
	do
json_select
lang_app_name=`json_select i18n && json_select app && json_select ${app} && json_get_var lang "$lang" && echo $lang`
json_select
json_select apps && json_select ${type} && json_select ${app} && json_get_var status status && json_get_var icon icon 2>&1 >/dev/null
[ "$status" = "active" ] && if
[ $icon -eq 1 ] 2>/dev/null
then
cat <<EOF
            <div class="pure-u-1-3 pure-u-md-1-6">
				<a href="index.cgi?app=${app}"><img src="/apps/${app}/icon.png"  alt="${lang_app_name}" /></a>
				<p><a class="content-subhead" href="index.cgi?app=${app}">${lang_app_name}</a></p>
            </div>

EOF
else
cat <<EOF
            <div class="pure-u-1-3 pure-u-md-1-6">
				<div class="losticon">
					<a href="index.cgi?app=${app}"></a>
				</div>
				<p><a class="content-subhead" href="index.cgi?app=${app}">${lang_app_name}</a></p>
            </div>

EOF
fi
	done

done


cat <<EOF
		

        </div>

    </div>

EOF
footer_html
}

change()
{
. /usr/share/libubox/jshn.sh
json_cleanup
json_init
json_load "`cat /tmp/home.json`"
json_select "setting"
json_add_string "default_lang" $FORM_lang
json_select ..
json_dump > /tmp/home.json
if
main.sbin check_admin_session
then
main.sbin header_jump "$HTTP_REFERER"
exit 0
else
main.sbin header_jump "index.cgi?app=login"
exit 1
fi
}
do_disconnect()
{
killall ubus
/bin/busybox start-stop-daemon -S -b -x ubus listen
/bin/busybox start-stop-daemon -S -b -x ubus call network.interface.$FORM_netzone down
/bin/busybox start-stop-daemon -S -b -x ubus call network.interface.$FORM_netzone down
/bin/busybox start-stop-daemon -S -b -x ubus call network.interface.$FORM_netzone down
/bin/busybox start-stop-daemon -S -b -x ubus call network.interface.$FORM_netzone down
# killall ubus
echo "断开成功" | main.sbin output_json 0
}
do_connect()
{
killall ubus
/bin/busybox start-stop-daemon -S -b -x ubus listen
/bin/busybox start-stop-daemon -S -b -x ubus call network.interface.$FORM_netzone up
/bin/busybox start-stop-daemon -S -b -x ubus call network.interface.$FORM_netzone up
/bin/busybox start-stop-daemon -S -b -x ubus call network.interface.$FORM_netzone up
/bin/busybox start-stop-daemon -S -b -x ubus call network.interface.$FORM_netzone up
# killall ubus
echo "开始尝试连接" | main.sbin output_json 0
}
main_status()
{
[ -f /var/run/brandwidth_get.pid ] && exit 1
echo $$ >/var/run/brandwidth_get.pid
uptimes=`awk '{printf("%d:%02d:%02d:%02d",($1/60/60/24),($1/60/60%24),($1/60%60),($1%60))}' /proc/uptime`
days=`echo $uptimes | awk -F ":" {'print $1'}`
hours=`echo $uptimes | awk -F ":" {'print $2'}`
mins=`echo $uptimes | awk -F ":" {'print $3'}`
secs=`echo $uptimes | awk -F ":" {'print $4'}`
cpuload="`busybox top -n 1 | grep -i "^CPU.*:" | awk {'print $2'} | sed 's/%.*/%/g' | grep -Eo "[0-9]*"`"
memload=`free | awk '/^Mem/ {printf("%u%", 100*$3/$2);}'`

printf "Content-Type: text/html; charset=utf-8\r\n\r\n"
cat <<'EOF'
<script>
function do_connection(netzone, action)
{
if (confirm("你是想 " + action + " " + netzone + " ?")) {
    var data = "app=home&action="+ action + "&netzone=" + netzone;
    var url = 'index.cgi';
    Ha.common.ajax(url, 'json', data, 'post', 'refresh_short_info');
  }
}
</script>
EOF
cat <<EOF

        <h2 class="content-head is-center">欢迎光临,以下是路由器近期相关信息</h2>

        <div class="pure-g">
            <div class="l-box pure-u-1 pure-u-md-1-2 pure-u-lg-1-4">

                <h3 class="content-subhead">
                    <i class="fa fa-rocket"></i>
                    网络状态
                </h3>


EOF

interface_json=`ubus call network.interface dump`
now=`date +%s`
. /usr/share/libubox/jshn.sh
json_init
json_load "$interface_json"
json_select "interface"
json_get_keys netzone_nums
brandwidth_old=`cat /tmp/brandwidth.json`
for netzone_num in $netzone_nums
do

netzone_prepare()
{
json_init
json_load "$interface_json"
json_select "interface"
json_select ${netzone_num}
json_get_vars "interface" "up" "uptime" "device" "proto"
echo "$interface" | grep -Eq "[a-z]6$" && continue
if
[ $up -eq 1 ]
then
# json_get_var "interface" "interface"
# json_get_var "up" "up"
# json_get_var "uptime" "uptime"
# json_get_var "device" "device"
# json_get_var "proto" "proto"
json_select
json_select "interface"
json_select ${netzone_num}
json_select "ipv4-address"
json_select 1 >/dev/null 2>&1
json_get_var "ipv4addr" "address"
# json_select .. && json_select .. && json_select .. && json_select ..
json_select
json_select "interface"
json_select ${netzone_num}
json_select "ipv6-address" >/dev/null 2>&1
json_select 1 >/dev/null 2>&1
json_get_var "ipv6addr" "address"
# json_select .. && json_select .. && json_select .. && json_select ..
json_select
json_select "interface"
json_select ${netzone_num}
json_select "route"
json_select 1 >/dev/null 2>&1
json_get_var gateway nexthop
# json_select .. && json_select .. && json_select .. && json_select ..
json_select
json_select "interface"
json_select ${netzone_num}
json_select "dns-server" >/dev/null 2>&1
json_get_values "dns"
json_select ..
json_select "dns-search" >/dev/null 2>&1
json_get_values "dns_search"

device_json=`ubus call network.device status '{ "name": "'$device'" }'`
json_init
json_load "$device_json"
json_select "statistics"
json_get_var upload "tx_bytes"
json_get_var download "rx_bytes"


json_init
json_load "$brandwidth_old"
json_get_var time_old "time"
json_select "$device"
json_get_var "upload_old" "upload"
json_get_var "download_old" "download"
upload_differ=`expr $upload - $upload_old`
download_differ=`expr $download - $download_old`
time_differ=`expr $now - $time_old`
upload_speed=$(expr $upload_differ / $time_differ)
download_speed=$(expr $download_differ / $time_differ)
num1=$(expr $upload_speed + $download_speed)
num2=$(expr 6 \* 131072)
brand_percent=`awk 'BEGIN{printf "%.2f\n",('$num1'/'$num2')*100}'`
[ -n "$download_speed" ] || continue

json_init
json_load "$brand_record"
json_add_string "time" "$now"
json_select "$device" >/dev/null 2>&1
if
[ $? -ne 0 ]
then
json_add_object "$device"
json_select "$device" >/dev/null 2>&1
fi
json_add_string "upload" "$upload"
json_add_string "download" "$download"
json_add_string "upload_speed" "$upload_speed"
json_add_string "download_speed" "$download_speed"
json_add_string "brand_percent" "$brand_percent"
upload_speed_per_s=`main.sbin storage_size_conver $upload_speed`
download_speed_per_s=`main.sbin storage_size_conver $download_speed`
upload_total=`main.sbin storage_size_conver $upload`
download_total=`main.sbin storage_size_conver $download`
else
brand_percent="0.00"
upload_speed_per_s="0 B"
download_speed_per_s="0 B"
upload_total="0 B"
download_total="0 B"
fi
brand_record=`json_dump`
}
netzone_prepare >/dev/null 2>&1

cat <<EOF
<div class="pure-g" style="white-space:nowrap;font-size:14px;">
  <div class="pure-g">
    <div class="pure-u-3-5" style="line-height: 14px">
		<b class="content-subhead">接口: $interface</b><br/>网卡: $device<br/>启用:$up<br/>IPv4:$ipv4addr<br/>IPv6:$ipv6addr<br/>网关: $gateway
	</div>
    <div class="pure-u-2-5" style="line-height: 14px;white-space:nowrap;font-size:14px;">
				协议:$proto<br/>已运行:$uptime<br/>dns: $dns<br/>dns-search: $dns_search<br/>↑:$upload_speed_per_s/s - $upload_total<br/>↓:$download_speed_per_s/s - $download_total
	</div>
  </div>
  <div class="pure-g">
    <div class="pure-u-4-5 progressdown">
		<div class="bar green">
			<div style="width: ${brand_percent}%">6M已用${brand_percent}%</div>
		</div>
	</div>
	<div class="pure-u-1-5">
EOF
if
[ $up -eq 1 ]
then
cat <<EOF
		<button onclick="do_connection('$interface','do_disconnect')" class="button-error pure-button">断开</button>
EOF
else
cat <<EOF
		<button onclick="do_connection('$interface','do_connect')" class="button-success pure-button">连接</button>
EOF
fi
cat <<EOF
	</div>
  </div>
</div>
EOF

done
echo "$brand_record" > /tmp/brandwidth.json

cat <<EOF

					
					
                
            </div>
            <div class="l-box pure-u-1 pure-u-md-1-2 pure-u-lg-1-4">
                <h3 class="content-subhead">
                    <i class="fa fa-mobile"></i>
                    网口状态
                </h3>
EOF

$DOCUMENT_ROOT/apps/vlan/vlan.sbin phystatus | tr '[A-Z]' '[a-z]' | while read port status
do
cat << EOF
<img src="/apps/vlan/imgs/eth-${status}.png"  alt="${status}" />
EOF
done

cat <<EOF
                <h3 class="content-subhead">
                    <i class="fa fa-mobile"></i>
                    在线人数
                </h3>
EOF

cat <<EOF
<table class="pure-table pure-table-bordered">
<thead>
<tr>
<td>
无线设备
</td>
<td>
在线人数
</td>
</tr>
</thead>
<tbody>
EOF
for radio in `ubus list | grep "^hostapd\."`
do
clients=`ubus call ${radio} get_clients | grep -Eo "..:.*:.[0-9a-z]" | wc -l`
cat <<EOF
<tr>
<td>
`echo ${radio} | sed 's/hostapd\.//'`
</td>
<td>
$clients
</td>
</tr>


EOF
done
cat <<EOF
</tbody>
</table>
EOF

cat <<EOF
            </div>
            <div class="l-box pure-u-1 pure-u-md-1-2 pure-u-lg-1-4">
                <h3 class="content-subhead">
                    <i class="fa fa-th-large"></i>
                    系统负载
                </h3>
<div>
<label>已运行</label>
<p>
$days 天 $hours 时 $mins 分 $secs 秒
</p>
</div>
<div class="pure-g" style="white-space:nowrap;font-size:14px;">
		<div class="pure-u-1-8" style="line-height: 22px">
			CPU:
		</div>
		<div class="pure-u-7-8 progressdown">
			<div class="bar green">
				<div style="width: ${cpuload}%">已用${cpuload}%</div>
			</div>
		</div>
</div>
<br/>
<div class="pure-g" style="white-space:nowrap;font-size:14px;">
    <div class="pure-u-1-8" style="line-height: 22px">
		内存:
	</div>
    <div class="pure-u-7-8 progressdown">
		<div class="bar green">
			<div style="width: ${memload}%">已用${memload}%</div>
		</div>
	</div>
</div>
<br/>
				

				
				
            </div>
            <div class="l-box pure-u-1 pure-u-md-1-2 pure-u-lg-1-4">
                <h3 class="content-subhead">
                    <i class="fa fa-check-square-o"></i>
                    最近事件
                </h3>
EOF

cat <<EOF

<table class="pure-table pure-table-horizontal">
    <thead>
        <tr>
            <th>通知</th>
        </tr>
    </thead>

    <tbody>
EOF
. $DOCUMENT_ROOT/apps/notice/notice_lib.sh
. /usr/share/libubox/jshn.sh
json_load "`cat /tmp/notice_index.json`"
json_get_keys notice_keys
notice_keys=`echo "$notice_keys" | grep -Eo "[0-9]*" | tail -n 10 | sort -nr`
for notice_key in $notice_keys
do
eval i18n_${notice_key}_desc="$(eval echo '$'JSON_TABLE${notice_key}_desc)"
[ -n "$(eval echo '$'i18n_${notice_key}_desc)" ] || eval i18n_${notice_key}_desc="$(eval echo '$'J_T${notice_key}_desc)"
done

json_load "`cat /tmp/notice_data.json`"
for notice_key in $notice_keys
do
json_select
json_select ${notice_key} >/dev/null 2>&1
json_select "variable" >/dev/null 2>&1
json_get_keys var_keys
for var_key in $var_keys
do
json_get_var "${var_key}" "${var_key}"
done
# desc_i18n=`eval echo $(eval echo '$'$(eval echo '$'i18n_${notice_key}_desc) | sed 's/{/${/g')`
desc_i18n=`eval echo '$'$(eval echo '$'i18n_${notice_key}_desc) | sed 's/{/${/g')`
cat <<EOF
<tr><td>`eval echo "$desc_i18n" 2>&1`</td></tr>
EOF
done
cat << EOF
        <tr>
		<td align="center"><a style="text-decoration:none;" class="content-subhead" href="/index.cgi?app=notice">查看更多</a></td>
        </tr>
    </tbody>
</table>


            </div>
        </div>


EOF
rm -f /var/run/brandwidth_get.pid
}

do_restart_os()
{
if
echo "$HTTP_USER_AGENT" | grep -q "^curl"
then
printf "Content-Type: text/html; charset=utf-8\r\n\r\n"
cat <<EOF
{"status": 0,"msg": "正在重启系统..."}
EOF
else
printf "Content-Type: text/html; charset=utf-8\r\n\r\n"
cat <<'EOF'
<script>
function resrart_os_refresh()
{
   window.location.reload();
}
setTimeout('resrart_os_refresh()',100000);
</script>
EOF
echo "正在重启系统..."
fi
/bin/busybox start-stop-daemon -S -b -x /sbin/reboot
}
do_restart_firewall()
{
if
echo "$HTTP_USER_AGENT" | grep -q "^curl"
then
printf "Content-Type: text/html; charset=utf-8\r\n\r\n"
cat <<EOF
{"status": 0,"msg": "正在重启防火墙..."}
EOF
else
printf "Content-Type: text/html; charset=utf-8\r\n\r\n"
cat <<'EOF'
<script>
function resrart_firewall_refresh()
{
   window.location.reload();
}
setTimeout('resrart_firewall_refresh()',10000);
</script>
EOF
echo "正在重启防火墙..."
fi
/bin/busybox start-stop-daemon -S -b -x /etc/init.d/firewall restart
}
do_list_firewall()
{
if
echo "$HTTP_USER_AGENT" | grep -q "^curl"
then
printf "Content-Type: text/html; charset=utf-8\r\n\r\n"
iptables-save
else
printf "Content-Type: text/html; charset=utf-8\r\n\r\n"
cat <<'EOF'
<script>
$(function(){
  $('#do_restart_firewall').on('click', function(e){
  var url = 'index.cgi?app=home&action=do_restart_firewall';
  Ha.common.ajax(url, 'html', '', 'get', '', function(data){
    $('#quick_tool').html(data);
  }, 1);
});
});
$(function(){
  $('#do_list_firewall').on('click', function(e){
  var url = 'index.cgi?app=home&action=do_list_firewall';
  Ha.common.ajax(url, 'html', '', 'get', '', function(data){
    $('#quick_tool').html(data);
  }, 1);
});
});
</script>
EOF

cat <<EOF
<h3 style="color:white">当前防火墙规则</h3>
<pre>`iptables-save`</pre>
<button class="pure-button" id="do_list_firewall">当前防火墙规则</button>
<button class="pure-button button-warning" id="do_restart_firewall">重启防火墙</button>
EOF
fi
}
do_restart_network()
{
if
echo "$HTTP_USER_AGENT" | grep -q "^curl"
then
printf "Content-Type: text/html; charset=utf-8\r\n\r\n"
cat <<EOF
{"status": 0,"msg": "正在重启网络..."}
EOF
else
printf "Content-Type: text/html; charset=utf-8\r\n\r\n"
cat <<'EOF'
<script>
function restart_network_refresh()
{
   window.location.reload();
}
setTimeout('restart_network_refresh()',10000);
</script>
EOF
echo "正在重启网络..."
fi
/bin/busybox start-stop-daemon -S -b -x /etc/init.d/network restart
}
get_mem_used()
{
mem_str=`grep -Ee '^(MemTotal|MemFree|Buffers|Cached|Active|Inactive):' /proc/meminfo | sed -e 's/^/"/g' -e 's/\:[ ]*/":"/g' -e 's/$/",/g' | tr '\n' ' '`
Percent=$(free | awk '/^Mem/ {printf("%u%%", 100*$3/$2);}')
. /usr/share/libubox/jshn.sh
json_load '{'"$mem_str"'"Percent":"'$Percent'"}'
printf "Content-Type: text/html; charset=utf-8\r\n\r\n"
json_dump
}
do_clean_mem()
{
if
echo "$HTTP_USER_AGENT" | grep -q "^curl"
then
echo 3 > /proc/sys/vm/drop_caches
printf "Content-Type: text/html; charset=utf-8\r\n\r\n"
cat <<EOF
{"status": 0,"msg": "清理内存成功."}
EOF
else
printf "Content-Type: text/html; charset=utf-8\r\n\r\n"
cat <<EOF
<h3 style="color:white">清理前</h3>
<pre>
EOF
grep -Ee '^(MemTotal|MemFree|Buffers|Cached|Active|Inactive):' /proc/meminfo
echo "Percent:		`free | awk '/^Mem/ {printf("%u%%", 100*$3/$2);}'`"
echo "</pre>"
echo 3 > /proc/sys/vm/drop_caches
cat <<EOF
<h3 style="color:white">清理后</h3>
<pre>
EOF
grep -Ee '^(MemTotal|MemFree|Buffers|Cached|Active|Inactive):' /proc/meminfo
echo "Percent:		`free | awk '/^Mem/ {printf("%u%%", 100*$3/$2);}'`"
echo "</pre>"
fi
}
do_netstat()
{
printf "Content-Type: text/html; charset=utf-8\r\n\r\n"
cat <<EOF

EOF
}
pre_restart_os()
{
printf "Content-Type: text/html; charset=utf-8\r\n\r\n"
cat <<'EOF'
<script>
$(function(){
  $('#do_restart_os').on('click', function(e){
  var url = 'index.cgi?app=home&action=do_restart_os';
  Ha.common.ajax(url, 'html', '', 'get', '', function(data){
    $('#quick_tool').html(data);
  }, 1);
});
});
</script>
EOF

awk '{printf("%d:%02d:%02d:%02d",($1/60/60/24),($1/60/60%24),($1/60%60),($1%60))}' /proc/uptime

cat <<EOF
<h3 style="color:white">重启系统</h3>
<p>是否重启整个系统?</p>
<button class="pure-button button-warning" id="do_restart_os">重启</button>
EOF
}
pre_system_log()
{
printf "Content-Type: text/html; charset=utf-8\r\n\r\n"
cat <<EOF
<h3 style="color:white">系统日志</h3>
<pre>
EOF
logread | tail -n 20
echo '</pre>'
}
pre_resrart_firewall()
{
printf "Content-Type: text/html; charset=utf-8\r\n\r\n"
cat <<'EOF'
<script>
$(function(){
  $('#do_restart_firewall').on('click', function(e){
  var url = 'index.cgi?app=home&action=do_restart_firewall';
  Ha.common.ajax(url, 'html', '', 'get', '', function(data){
    $('#quick_tool').html(data);
  }, 1);
});
});
$(function(){
  $('#do_list_firewall').on('click', function(e){
  var url = 'index.cgi?app=home&action=do_list_firewall';
  Ha.common.ajax(url, 'html', '', 'get', '', function(data){
    $('#quick_tool').html(data);
  }, 1);
});
});
</script>
EOF

cat <<EOF
<h3 style="color:white">重启防火墙</h3>
<p>是否重启整个防火墙?</p>
<button class="pure-button" id="do_list_firewall">当前防火墙规则</button>
<button class="pure-button button-warning" id="do_restart_firewall">重启防火墙</button>
EOF
}
pre_restart_network()
{
printf "Content-Type: text/html; charset=utf-8\r\n\r\n"
cat <<'EOF'
<script>
$(function(){
  $('#do_restart_network').on('click', function(e){
  var url = 'index.cgi?app=home&action=do_restart_network';
  Ha.common.ajax(url, 'html', '', 'get', '', function(data){
    $('#quick_tool').html(data);
  }, 1);
});
});
</script>
EOF
cat <<EOF
<h3 style="color:white">重启网络</h3>
<p>是否重启整个网络?</p>
<button class="pure-button button-warning" id="do_restart_network">重启网络</button>
EOF
}
pre_clean_mem()
{
cat <<'EOF'

<script>
$(function(){
  $('#do_clean_mem').on('click', function(e){
  var url = 'index.cgi?app=home&action=do_clean_mem';
  Ha.common.ajax(url, 'html', '', 'get', '', function(data){
    $('#quick_tool').html(data);
  }, 1);
});
});
</script>
EOF
cat <<EOF
<h3 style="color:white">清理前</h3>
<pre>
EOF
grep -Ee '^(MemTotal|MemFree|Buffers|Cached|Active|Inactive):' /proc/meminfo
echo "Percent:		`free | awk '/^Mem/ {printf("%u%%", 100*$3/$2);}'`"
cat <<EOF
</pre>
<button class="pure-button button-warning" id="do_clean_mem">清理</button>
EOF
}
do_netstat_tcp_udp()
{
cat <<'EOF'

<script>
$(function(){
  $('#do_netstat_tcp_udp').on('click', function(e){
  var url = 'index.cgi?app=home&action=do_netstat_tcp_udp';
  Ha.common.ajax(url, 'html', '', 'get', '', function(data){
    $('#quick_tool').html(data);
  }, 1);
});
});
$(function(){
  $('#do_netstat_tcp_only').on('click', function(e){
  var url = 'index.cgi?app=home&action=do_netstat_tcp_only';
  Ha.common.ajax(url, 'html', '', 'get', '', function(data){
    $('#quick_tool').html(data);
  }, 1);
});
});
$(function(){
  $('#do_netstat_udp_only').on('click', function(e){
  var url = 'index.cgi?app=home&action=do_netstat_udp_only';
  Ha.common.ajax(url, 'html', '', 'get', '', function(data){
    $('#quick_tool').html(data);
  }, 1);
});
});
</script>
EOF
cat <<EOF
<h3 style="color:white">TCP UDP 端口占用</h3>
EOF
str=`netstat -tulnp | grep -vE "Active Internet connections|Proto.*Recv-Q" | awk '{if ($6 == "LISTEN") print "<tr><td>"$1"</td><td>"$2"</td><td>"$3"</td><td>"$4"</td><td>"$5"</td><td>"$6"</td><td>"$7"</td></tr>" ; else print "<tr><td>"$1"</td><td>"$2"</td><td>"$3"</td><td>"$4"</td><td>"$5"</td><td>-</td><td>"$6"</td></tr>"}'`
echo '<table class="pure-table"><thead><tr><td>Proto</td><td>Recv-Q</td><td>Send-Q</td><td>Local Address</td><td>Foreign Address</td><td>State</td><td>PID/Program name</td></tr></thead><tbody>'"$str"'</tbody></table>';
cat <<EOF
<br/>
<button class="pure-button" id="do_netstat_tcp_udp">tcp_udp</button>
<button class="pure-button button-secondary" id="do_netstat_tcp_only">tcp_only</button>
<button class="pure-button button-secondary" id="do_netstat_udp_only">udp_only</button>
EOF
}
do_netstat_tcp_only()
{
printf "Content-Type: text/html; charset=utf-8\r\n\r\n"
cat <<'EOF'
<script>
$(function(){
  $('#do_netstat_tcp_udp').on('click', function(e){
  var url = 'index.cgi?app=home&action=do_netstat_tcp_udp';
  Ha.common.ajax(url, 'html', '', 'get', '', function(data){
    $('#quick_tool').html(data);
  }, 1);
});
});
$(function(){
  $('#do_netstat_tcp_only').on('click', function(e){
  var url = 'index.cgi?app=home&action=do_netstat_tcp_only';
  Ha.common.ajax(url, 'html', '', 'get', '', function(data){
    $('#quick_tool').html(data);
  }, 1);
});
});
$(function(){
  $('#do_netstat_udp_only').on('click', function(e){
  var url = 'index.cgi?app=home&action=do_netstat_udp_only';
  Ha.common.ajax(url, 'html', '', 'get', '', function(data){
    $('#quick_tool').html(data);
  }, 1);
});
});
</script>
EOF
cat <<EOF
<h3 style="color:white">TCP 端口占用</h3>
EOF
str=`netstat -tlnp | grep -vE "Active Internet connections|Proto.*Recv-Q" | awk '{if ($6 == "LISTEN") print "<tr><td>"$1"</td><td>"$2"</td><td>"$3"</td><td>"$4"</td><td>"$5"</td><td>"$6"</td><td>"$7"</td></tr>" ; else print "<tr><td>"$1"</td><td>"$2"</td><td>"$3"</td><td>"$4"</td><td>"$5"</td><td>-</td><td>"$6"</td></tr>"}'`
echo '<table class="pure-table"><thead><tr><td>Proto</td><td>Recv-Q</td><td>Send-Q</td><td>Local Address</td><td>Foreign Address</td><td>State</td><td>PID/Program name</td></tr></thead><tbody>'"$str"'</tbody></table>';
cat <<EOF
<br/>
<button class="pure-button" id="do_netstat_tcp_udp">tcp_udp</button>
<button class="pure-button button-secondary" id="do_netstat_tcp_only">tcp_only</button>
<button class="pure-button button-secondary" id="do_netstat_udp_only">udp_only</button>
EOF
}
do_netstat_udp_only()
{
printf "Content-Type: text/html; charset=utf-8\r\n\r\n"
cat <<'EOF'
<script>
$(function(){
  $('#do_netstat_tcp_udp').on('click', function(e){
  var url = 'index.cgi?app=home&action=do_netstat_tcp_udp';
  Ha.common.ajax(url, 'html', '', 'get', '', function(data){
    $('#quick_tool').html(data);
  }, 1);
});
});
$(function(){
  $('#do_netstat_tcp_only').on('click', function(e){
  var url = 'index.cgi?app=home&action=do_netstat_tcp_only';
  Ha.common.ajax(url, 'html', '', 'get', '', function(data){
    $('#quick_tool').html(data);
  }, 1);
});
});
$(function(){
  $('#do_netstat_udp_only').on('click', function(e){
  var url = 'index.cgi?app=home&action=do_netstat_udp_only';
  Ha.common.ajax(url, 'html', '', 'get', '', function(data){
    $('#quick_tool').html(data);
  }, 1);
});
});
</script>
EOF
cat <<EOF
<h3 style="color:white">UDP 端口占用</h3>
EOF
str=`netstat -ulnp | grep -vE "Active Internet connections|Proto.*Recv-Q" | awk '{if ($6 == "LISTEN") print "<tr><td>"$1"</td><td>"$2"</td><td>"$3"</td><td>"$4"</td><td>"$5"</td><td>"$6"</td><td>"$7"</td></tr>" ; else print "<tr><td>"$1"</td><td>"$2"</td><td>"$3"</td><td>"$4"</td><td>"$5"</td><td>-</td><td>"$6"</td></tr>"}'`
echo '<table class="pure-table"><thead><tr><td>Proto</td><td>Recv-Q</td><td>Send-Q</td><td>Local Address</td><td>Foreign Address</td><td>State</td><td>PID/Program name</td></tr></thead><tbody>'"$str"'</tbody></table>';
cat <<EOF
<br/>
<button class="pure-button" id="do_netstat_tcp_udp">tcp_udp</button>
<button class="pure-button button-secondary" id="do_netstat_tcp_only">tcp_only</button>
<button class="pure-button button-secondary" id="do_netstat_udp_only">udp_only</button>
EOF
}
pre_netstat()
{
printf "Content-Type: text/html; charset=utf-8\r\n\r\n"
do_netstat_tcp_udp
}



lang=`main.sbin get_client_lang`
eval `cat $DOCUMENT_ROOT/apps/$FORM_app/i18n/$lang/i18n.conf`
if
[ $is_main_page = 1 ]
then
main
elif [ -n "$FORM_action" ]
then
$FORM_action
fi
